package org.daidai.abstrusewriting.homepage.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.top.FocusImageActivity;
import org.daidai.abstrusewriting.homepage.top.adapters.RankingListAdapter;
import org.daidai.abstrusewriting.homepage.top.model.RankingList;
import org.daidai.abstrusewriting.homepage.top.model.TopModel;
import org.daidai.abstrusewriting.homepage.top.service.TopService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TopFragment extends BaseFragment implements View.OnClickListener {

    private TopModel mTopModel;
    private TopService topService;
    private static final String TAG = "TopFragment";
    private ImageView focusImage;
    private ListView topListView;
    private RankingListAdapter adapter;
    private List<RankingList> datas;

    public TopFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return "TopFragment";
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        datas = new ArrayList<>();

        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://mobile.ximalaya.com/mobile/discovery/v2/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                .build();

        topService = retrofit.create(TopService.class);
        Observable<TopModel> observable = topService.getTopDetail();
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TopModel>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(TopModel topModel) {
                        String msg = topModel.getMsg();
                        Log.d("TopFragment", "onNext: " + msg);
                        if ("成功".equals(msg)) {
                            mTopModel = topModel;
                            if (mTopModel != null) {
                                Log.d(TAG, "onCreateView: ");
                                Picasso.with(getContext()).load(mTopModel.getFocusImage().getPics().get(0).getPic()).into(focusImage);
                                datas.clear();
                                datas.addAll(mTopModel.getDatas());
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(getContext(), "未获取到数据", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_top, container, false);
        focusImage = (ImageView) view.findViewById(R.id.top_focusImage);
        focusImage.setOnClickListener(this);
        topListView = (ListView) view.findViewById(R.id.top_list);
        adapter = new RankingListAdapter(getContext(), datas);
        topListView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onClick(View v) {
        String url = mTopModel.getFocusImage().getPics().get(0).getUrl();
        Intent intent = new Intent(getContext(), FocusImageActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }
}
